package br.edu.ufcg.dsc.ia1.jt.util;

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Essa classe lineariza as figuras de entrada preenchendo pixels missing
 * por uma cor de fundo definida pelo usuário
 * @author theo
 *
 */
public class RasterAsLine implements Iterable<Integer>{
	
	List<Integer> line;
	double[] dline;
	
	public RasterAsLine(BufferedImage img) {
		this(img, img.getHeight(), img.getWidth(), 0);
	}
	
	public RasterAsLine(BufferedImage img, int height, int width, int background) {
		this.serialize(img, height, width, background);
	}
	
	private void serialize(BufferedImage img, int height, int width, int normal) {
		Raster raster = img.getData();
		this.dline = new double[width * height];
		this.line = new ArrayList<Integer>();
		int i = 0;
		for(int y = 0; y < height; y++) {
			for(int x = 0; x < width; x++) {
				int pixel = y < img.getHeight() && x < img.getWidth() ? 
						raster.getPixel(x, y, new int[1])[0] : normal;
				this.line.add(pixel);
				this.dline[i++] = pixel;
			}
		}
	}
	
	public int pixelAt(int Nth) {
		return this.line.get(Nth);
	}
	
	public double[] line() {
		return this.dline;
	}

	@Override
	public Iterator<Integer> iterator() {
		return this.line.iterator();
	}
	
}